package com.hanbang.gycatering.aboutme;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.hanbang.gangyu.R;
import com.hanbang.gycatering.application.MyApplication;
import com.hanbang.gycatering.bean.CommodityData;
import com.hanbang.gycatering.bean.OrderData;
import com.hanbang.gycatering.bean.RepairBeanData;
import com.hanbang.gycatering.comeon.RegisterActivity;
import com.hanbang.gycatering.homepage.BaseActivity;
import com.hanbang.gycatering.myviews.MyDialog;
import com.hanbang.gycatering.myviews.MyDialog.MODE;
import com.hanbang.gycatering.myviews.MyDialog.OnClickCallback;
import com.hanbang.gycatering.order.OrderFragment;
import com.hanbang.gycatering.utils.HttpInterfaces;
import com.hanbang.gycatering.utils.MyLog;
import com.hanbang.gycatering.utils.MyRequestCallBack;
import com.hanbang.gycatering.utils.MyToast;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;

public class AddressManage extends BaseActivity {
	private EditText myaddress_name, myaddress_phone, myaddress_detailaddress;
	private TextView myaddress_plot, sumbit_address;
	private LinearLayout ll_myaddress_plot;
	private int plotId = -1;
	private ArrayList<Object> addressDatas = new ArrayList<Object>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.addressmanage);
		initView();
		setTheListener();
		getTheAddress();
		getPlotData();
	}

	private void initView() {
		// TODO Auto-generated method stub
		((TextView) findViewById(R.id.top_bar_title)).setText("修改收货地址");
		myaddress_name = (EditText) findViewById(R.id.myaddress_name);
		myaddress_phone = (EditText) findViewById(R.id.myaddress_phone);
		myaddress_detailaddress = (EditText) findViewById(R.id.myaddress_detailaddress);
		myaddress_plot = (TextView) findViewById(R.id.myaddress_plot);
		sumbit_address = (TextView) findViewById(R.id.sumbit_address);
		ll_myaddress_plot = (LinearLayout) findViewById(R.id.ll_myaddress_plot);
	}

	private void setTheListener() {
		// TODO Auto-generated method stub
		setBackArrows((ImageView) findViewById(R.id.top_bar_left_image));
		ll_myaddress_plot.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				MyDialog dialog = new MyDialog(AddressManage.this,
						R.style.about_me_dialog, MODE.SELECT) {
				};
				dialog.setTitle("请选择您所在小区");
				dialog.setOnClickCallback(new OnClickCallback() {
					@Override
					public void onClick(MyDialog dialog, Object number,
							Object content) {
						if ("-1".equals(String.valueOf(number))) {
							myaddress_plot.setText(String.valueOf("请选择您所在小区"));
						} else {
							myaddress_plot
									.setText(((RepairBeanData) addressDatas
											.get((Integer) number)).getName());
							plotId = ((RepairBeanData) addressDatas
									.get((Integer) number)).getId();
						}

					}
				});
				dialog.setSelectData(addressDatas);
				dialog.show();
			}
		});
		sumbit_address.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				boolean Sign1 = myaddress_name.getText().length() > 0;
				boolean Sign2 = myaddress_phone.getText().length() > 0;
				boolean Sign3 = myaddress_detailaddress.getText().length() > 0;
				boolean Sign4 = (plotId != -1);
				MyLog.e("onTextChanged", myaddress_plot.getText().toString().trim());
				if (Sign1 & Sign2 & Sign3 & Sign4) {
					MyLog.e("onTextChanged", "真的");
					sumbitAddress();
				}else{
					MyToast.show(AddressManage.this, "请将信息填写完整",
							Toast.LENGTH_SHORT);
				}
				
			}
		});
//		// 为EditText绑定监听器
//		textChange tc1 = new textChange();
//		myaddress_name.addTextChangedListener(tc1);
//		myaddress_phone.addTextChangedListener(tc1);
//		myaddress_plot.addTextChangedListener(tc1);
//		myaddress_detailaddress.addTextChangedListener(tc1);
	}

	// 获取原来地址
	private void getTheAddress() {
		// TODO Auto-generated method stub
		
		((MyApplication) getApplicationContext()).httpUtils.send(
				HttpMethod.GET, HttpInterfaces.myaddress + "&userphone="
						+ userData.getUserphone() + "&password="
						+ userData.getUserpassword(),
				new MyRequestCallBack<String>(AddressManage.this,
						getString(R.string.loadding)) {
					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						super.onSuccess(responseInfo);
						try {
							JSONObject json = new JSONObject(
									responseInfo.result);
							if (json.getInt("state") == 1) {
								myaddress_name.setText(json.getString("name"));
								myaddress_phone.setText(json.getString("phone"));
								myaddress_detailaddress.setText(json
										.getString("address"));
								myaddress_plot.setText(json
										.getString("plotname"));
								plotId = json.getInt("plotid");
							} else {
								MyToast.show(AddressManage.this, "获取地址失败",
										Toast.LENGTH_SHORT);
							}
						} catch (JSONException e) {
							e.printStackTrace();
							MyToast.show(AddressManage.this, "获取地址失败",
									Toast.LENGTH_SHORT);
						}
					}
				});

	}

	// 提交修改地址
	private void sumbitAddress() {
		// TODO Auto-generated method stub
		MyLog.e("sumbitAddress", HttpInterfaces.updateadderss+"&userphone="
		+userData.getUserphone()+"&password="+userData.getUserpassword()
		+"&realname="+myaddress_name.getText().toString().trim()
		+"&cellphone="+myaddress_phone.getText().toString().trim()+"&plotid="+plotId
		+"&house="+myaddress_detailaddress.getText().toString().trim());
		((MyApplication) getApplicationContext()).httpUtils.send(
				HttpMethod.GET, HttpInterfaces.updateadderss+"&userphone="
		+userData.getUserphone()+"&password="+userData.getUserpassword()
		+"&realname="+myaddress_name.getText().toString().trim()
		+"&cellphone="+myaddress_phone.getText().toString().trim()+"&plotid="+plotId
		+"&house="+myaddress_detailaddress.getText().toString().trim(),
				new MyRequestCallBack<String>(AddressManage.this,
						getString(R.string.loadding)) {
					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						super.onSuccess(responseInfo);
						try {
							JSONObject json = new JSONObject(
									responseInfo.result);
							if (json.getInt("state") == 1) {
								MyToast.show(AddressManage.this, "提交成功",
										Toast.LENGTH_SHORT);
								finish();
							} else {
								MyToast.show(AddressManage.this, "提交失败",
										Toast.LENGTH_SHORT);
							}
						} catch (JSONException e) {
							e.printStackTrace();
							MyToast.show(AddressManage.this, "获取数据失败3",
									Toast.LENGTH_SHORT);
						}
					}
				});
	}

	// 获取小区数据
	private void getPlotData() {
		// TODO Auto-generated method stub
		((MyApplication) getApplicationContext()).httpUtils.send(
				HttpMethod.GET, HttpInterfaces.getplot,
				new MyRequestCallBack<String>(AddressManage.this,
						getString(R.string.loadding)) {
					@Override
					public void onSuccess(ResponseInfo<String> responseInfo) {
						super.onSuccess(responseInfo);
						try {
							JSONObject json = new JSONObject(
									responseInfo.result);
							if (json.getInt("state") == 1) {
								JSONArray plot = json.getJSONObject("info")
										.getJSONArray("plot");
								for (int i = 0; i < plot.length(); i++) {
									RepairBeanData repair = new RepairBeanData();
									repair.setId(plot.getJSONObject(i).getInt(
											"id"));
									repair.setName(plot.getJSONObject(i)
											.getString("name"));
									addressDatas.add(repair);
								}

							} else {
								MyToast.show(AddressManage.this, "没能获取小区",
										Toast.LENGTH_SHORT);
							}
						} catch (JSONException e) {
							e.printStackTrace();
							MyToast.show(AddressManage.this, "没能获取小区",
									Toast.LENGTH_SHORT);
						}
					}
				});
	}

//	// EditText监听器
//	class textChange implements TextWatcher {
//
//		@Override
//		public void afterTextChanged(Editable arg0) {
//
//		}
//
//		@Override
//		public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
//				int arg3) {
//
//		}
//
//		@SuppressLint("NewApi")
//		@Override
//		public void onTextChanged(CharSequence cs, int start, int before,
//				int count) {
//			boolean Sign1 = myaddress_name.getText().length() > 0;
//			boolean Sign2 = myaddress_phone.getText().length() > 0;
//			boolean Sign3 = myaddress_detailaddress.getText().length() > 0;
//			boolean Sign4 = (plotId != -1);
//			MyLog.e("onTextChanged", myaddress_plot.getText().toString().trim());
//			if (Sign1 & Sign2 & Sign3 & Sign4) {
//				myaddress_plot.setEnabled(true);
//				MyLog.e("onTextChanged", "真的");
//				sumbit_address.setTextColor(getResources().getColor(
//						R.color.white));
//				sumbit_address.setBackground(getResources().getDrawable(
//						R.drawable.button_submit));
//			}
//			// 在layout文件中，对Button的text属性应预先设置默认值，否则刚打开程序的时候Button是无显示的
//			else {
//				MyLog.e("onTextChanged", "假的");
//				sumbit_address.setTextColor(getResources().getColor(
//						R.color.black));
//				sumbit_address.setBackground(getResources().getDrawable(
//						R.drawable.button_myshape));
//				myaddress_plot.setEnabled(false);
//			}
//		}
//
//	}

}
